import { _decorator, Animation, animation, AnimationClip, AnimationState, Component, director, EPhysics2DDrawFlags, Input, input, instantiate, KeyCode, Node, PhysicsSystem2D, Prefab } from 'cc';
import { enemy } from './enemy';
import { syMoveButten } from './syMoveButten';
import { bgMapManager } from './bgMapManager';
const { ccclass, property } = _decorator;

const put = {};
const state = {
    stand: 1,
    attack: 0,
}

@ccclass('hero')

export class hero extends Component {
    @property(Node)
    enemy: Node
    // @property(syMoveButten)
    // symoveButten: syMoveButten
    @property(bgMapManager)
    bgmap: bgMapManager
    @property
    speend = 200;


    index = 0;
    anima = "idle";
    herostate = state.stand;
    combo = 0;
    //攻击判定
    heroattck = 0
    enemyhurt = 0
    //判定是否碰撞
    isHit = false;


    start() {

        input.on(Input.EventType.KEY_DOWN, this.onKeydown, this);
        input.on(Input.EventType.KEY_UP, this.onKeyup, this);

        let animation = this.node.getComponent(Animation);
        animation.on(Animation.EventType.FINISHED, this.onAnimationEvent, this)
    }

    onAnimationEvent(e, data) {
        if (data.name == 'attack1' || data.name == 'attack2' || data.name == 'attack3') {
            this.herostate = state.stand;
            this.combo = (this.combo + 1) % 3;
            this.setAnima('idle');
            //攻击间隔设定
            setTimeout(() => {
                if (this.herostate == state.attack) return;
                this.combo = 0;
            }, 500);
        }
    }

    protected onDestroy(): void {
        input.off(Input.EventType.KEY_DOWN, this.onKeydown, this);
        input.off(Input.EventType.KEY_UP, this.onKeyup, this);
    }

    onKeydown(e) {
        switch (e.keyCode) {
            case KeyCode.KEY_A:
                this.index = -1;
                break;
            case KeyCode.KEY_D:
                this.index = 1;
                break;
            case KeyCode.KEY_J:
                this.herostate = state.attack;
                this.enemyhurt = 1
                break;
        }
    }
    onKeyup(e) {
        switch (e.keyCode) {
            case KeyCode.KEY_A:
                this.index = 0;
                break;
            case KeyCode.KEY_D:
                this.index = 0;
                break;

        }
    }

    //动画播放
    setAnima(anima) {
        if (this.anima == anima) return;
        const animationComponent = this.node.getComponent(Animation);
        this.anima = anima;
        animationComponent.play(anima);
    }

    enemyHit() {
        let x = 0
        for (let i = 0; i < this.enemy.children.length; i++) {
            const enemyChild = this.enemy.children[i]
            if (enemyChild.isValid) {
                x = this.node.worldPosition.x - enemyChild.worldPosition.x
                if (x < 20 && x > -20) {
                    if (this.enemyhurt) {
                        enemyChild.getComponent(enemy).isHit = true
                        // this.isHit = true
                        this.enemyhurt = 0
                    }
                    // this.isHit = false
                }
            }
        }
    }

    initMove(deltaTime) {
        //移动角色
        if (this.index) {
            const x = this.node.position.x + this.index * this.speend * deltaTime;
            //if(x>-350&&x<350){
            //反转角色
            let scaleX = Math.abs(this.node.getScale().x);
            this.node.setScale(this.index * scaleX, this.node.getScale().y);
            //改变位置
            this.node.setPosition(x, this.node.position.y);
            //调用动画方法
            // anim = "run";
            this.setAnima("run");
            // }
        } else {
            // anim = "idle";
            this.setAnima("idle");
        }
    }

    update(deltaTime: number) {

        let anim = this.anima;
        //判断是否处于攻击状态
        if (this.herostate) {
            this.initMove(deltaTime)
        } else {
            if (this.combo == 0) {
                anim = 'attack1';
            } else if (this.combo == 1) {
                anim = 'attack2';
            } else if (this.combo == 2) {
                anim = 'attack3';
            }
            this.enemyHit()
            this.setAnima(anim);

        }
    }

    lateUpdate() {
        //判断是否切换场景
        if (this.node.position.x > 2570) {
            director.loadScene('bgMap1')
            this.bgmap.index = 0
        }
    }
}


